package cn.woblog.recyclerviewsample.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.woblog.recyclerviewsample.BaseRecyclerViewAdapter;
import cn.woblog.recyclerviewsample.R;
import cn.woblog.recyclerviewsample.view.SwipeMenuLayout;
import cn.woblog.recyclerviewsample.view.SwipeMenuLayout.OnSwipeListener;

/**
 * Created by Pich on 2017/4/4.
 */

public class SwipeRecyclerViewMenuAdapter extends
    BaseRecyclerViewAdapter<String, SwipeRecyclerViewMenuAdapter.ViewHolder> {

  private SwipeMenuLayout lastItem;

  public SwipeRecyclerViewMenuAdapter(Context context) {
    super(context);
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(getInflater().inflate(R.layout.item_swipe_menu, parent, false));
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
    super.onBindViewHolder(holder, position);
    holder.bindData(getData(position));
    holder.spl_container.setOnSwipeListener(new OnSwipeListener() {

      @Override
      public void onStartSwipe(SwipeMenuLayout swipeMenuLayout) {
        if (lastItem != null && lastItem.isSwiped()) {
          lastItem.close();
        }
      }

      @Override
      public void onSwiped(SwipeMenuLayout swipeMenuLayout) {
        lastItem = swipeMenuLayout;
      }

      @Override
      public void onClose(SwipeMenuLayout swipeMenuLayout) {
        lastItem = null;
      }
    });

    holder.iv_menu_delete.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        holder.spl_container.close();
        Toast.makeText(getContext(), "删除：" + position, Toast.LENGTH_SHORT).show();
      }
    });

    holder.iv_menu_top.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        holder.spl_container.close();
        Toast.makeText(getContext(), "置顶：" + position, Toast.LENGTH_SHORT).show();
      }
    });
//    holder.iv_icon.setOnTouchListener(new OnTouchListener() {
//      @Override
//      public boolean onTouch(View v, MotionEvent event) {
//        ((SwipeRecyclerViewActivity) getContext()).startDrag(holder);
//        return true;
//      }
//    });
//
//    holder.iv_delete.setOnTouchListener(new OnTouchListener() {
//      @Override
//      public boolean onTouch(View v, MotionEvent event) {
//        ((SwipeRecyclerViewActivity) getContext()).startSwipe(holder);
//        return true;
//      }
//    });
  }

  public class ViewHolder extends BaseRecyclerViewAdapter.ViewHolder {

    final ImageView iv_icon;
    final ImageView iv_delete;
    final SwipeMenuLayout spl_container;
    final TextView iv_menu_top;
    final TextView iv_menu_delete;
    private final TextView tv;

    public ViewHolder(View itemView) {
      super(itemView);
      spl_container = (SwipeMenuLayout) itemView.findViewById(R.id.spl_container);
      tv = (TextView) itemView.findViewById(android.R.id.text1);
      iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
      iv_delete = (ImageView) itemView.findViewById(R.id.iv_delete);
      iv_menu_delete = (TextView) itemView.findViewById(R.id.iv_menu_delete);
      iv_menu_top = (TextView) itemView.findViewById(R.id.iv_menu_top);
    }

    public void bindData(String data) {
      tv.setText(data);
    }
  }
}
